package com.thread.parallel;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.StampedLock;
import java.util.stream.Collectors;

/**
 * @author wangyingjie
 * @date
 * @remark
 */
public class ParallelDemo {

    public static void main(String[] args) throws InterruptedException {
        Integer[] arr = {1,2,3,4};
        List<Integer> arrayList = new ArrayList<>(Arrays.asList(arr));

        ArrayList<Integer> objects = new ArrayList<Integer>();
        CopyOnWriteArrayList<Integer> objects1 = new CopyOnWriteArrayList<>();
        //并行操作需要用到线程安全的容器
        arrayList.parallelStream().peek(e->objects1.add(e)).collect(Collectors.toList());
//        arrayList.stream().parallel().peek(e->objects.add(e)).collect(Collectors.toList());
//        Thread.sleep(1000);
        objects1.stream().forEach(System.out::println);

        //需要有返回结果的
        CompletableFuture<String> cf = CompletableFuture.supplyAsync(() -> method());

        StampedLock stampedLock = new StampedLock();
    }

    private static <String> String method() {
        return null;
    }
}
